/*
 * KKContactListener.cpp
 *
 *  Created on: 2013-9-15
 *      Author: wanghua
 */

#include "KKContactListener.h"
#include "KKNode.h"
#include "KKSprite.h"

KKContactListener* KKContactListener::instance = NULL;
KKContactListener::KKContactListener(void) {
}

KKContactListener::~KKContactListener(void) {
}

KKContactListener* KKContactListener::sharedListener() {
	if (!instance) {
		instance = new KKContactListener();
	}
	return instance;
}

void KKContactListener::performContactEvent(b2Body* a, b2Body* b) {
	CCNode* contactNodeA = (CCNode*) (a->GetUserData());
	KKNode* contactNodeB = dynamic_cast<KKNode*>((CCNode*) (b->GetUserData()));
	if (KKSprite* psA = dynamic_cast<KKSprite*>(contactNodeA)) {

		psA->onContact(contactNodeB);
	}
}

void KKContactListener::BeginContact(b2Contact* contact) {
	b2Body* bodyA = contact->GetFixtureA()->GetBody();
	b2Body* bodyB = contact->GetFixtureB()->GetBody();
	performContactEvent(bodyA, bodyB);
	performContactEvent(bodyB, bodyA);

}

void KKContactListener::EndContact(b2Contact* contact) {
	b2Body* bodyA = contact->GetFixtureA()->GetBody();
	b2Body* bodyB = contact->GetFixtureB()->GetBody();

}
